package com.dingreading.cloud.admin.controller;

import com.dingreading.cloud.admin.entity.AdminMenus;
import com.dingreading.cloud.admin.entity.AdminRoleMenu;
import com.dingreading.cloud.admin.service.AdminMenusService;
import com.dingreading.cloud.admin.service.AdminRoleMenuService;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.DescribeException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/system/adminRoleMenu")
@Api(tags = "总部-总部角色功能关联菜单管理")
public class AdminRoleMenuController {

    @Resource
    protected AdminMenusService menusService;
    @Resource
    protected AdminRoleMenuService roleMenuService;


    @ApiOperation(value = "根据角色获取菜单列表")
    @PostMapping(value = "/getByRoleId", produces = "application/json; charset=UTF-8")
    public R<Object> getByRoleId(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "角色id") @RequestParam(value = "roleId") Long roleId
    ) {
        List<AdminMenus> list = menusService.getByRoleIdAndEnabled(roleId, null);
        return R.ok(list);
    }


    @Transactional(rollbackFor = DescribeException.class)
    @ApiOperation(value = "添加/删除用户到角色功能")
    @PostMapping(value = "/newBean", produces = "application/json; charset=UTF-8")
    public R<Object> newBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "角色id") @RequestParam(value = "roleId") Long roleId,
            @ApiParam(value = "菜单id集合") @RequestParam(value = "ids", required = false) List<Long> ids
    ) {
        if (CollectionUtils.isNotEmpty(ids)) {
            List<AdminRoleMenu> exList = roleMenuService.getByRoleId(roleId);
            if (CollectionUtils.isEmpty(exList)) {
                // 新增
                List<AdminRoleMenu> newList = new ArrayList<>();
                for (Long id : ids) {
                    AdminRoleMenu data = new AdminRoleMenu();
                    data.setRoleId(roleId);
                    data.setMenuId(id);
                    newList.add(data);
                }
                roleMenuService.saveOrUpdateBatch(newList);
            } else {
                // 修改
                List<AdminRoleMenu> newList = new ArrayList<>();
                List<Long> exMenuIds = exList.stream().map(AdminRoleMenu::getMenuId).collect(Collectors.toList());
                for (Long id : ids) {
                    if (!exMenuIds.contains(id)) {
                        AdminRoleMenu data = new AdminRoleMenu();
                        data.setRoleId(roleId);
                        data.setMenuId(id);
                        newList.add(data);
                    }
                }
                // 删除不要的菜单数据
                roleMenuService.dropByRoleIdNotMenuIds(roleId, ids);
                // 保存
                if (CollectionUtils.isNotEmpty(newList))
                    roleMenuService.saveOrUpdateBatch(newList);
            }
        } else {
            // 删除全部
            long l = roleMenuService.dropByRoleId(roleId);
        }
        return R.ok();
    }

}
